package com.jerei.plugin.${packageFolder}.control;

import com.jerei.plugin.entity.${entityName};
import com.jerei.plugin.service.I${entityName}Service;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.spring.PageBean;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import com.jerei.utils.UtilsCondition;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;
/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
public class ${entityName}Control extends PluginController {

    @Autowired
    private I${entityName}Service i${entityName}Service;

    @RequiresPermissions({"*:save", "*:update"})
    public String save() {
        ${entityName} entity = this.wrapper(${entityName}.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            i${entityName}Service.update(entity);
        } else {
            i${entityName}Service.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"*:remove"})
    public String delete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                i${entityName}Service.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"*:save","*:update","*:view"})
    public void edit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        ${entityName} entity = null;
        if(null!=id){
            entity = i${entityName}Service.find(id);
        }else{
            entity = new ${entityName}();
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"*:view"})
    public String page() {
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        PageBean<${entityName}> pageBean = i${entityName}Service.page(nowPage, pageSize,UtilsCondition.getConditionEntity(this.getRequest().getParameterMap()));
        return UtilsUI.datagrid(pageBean);
    }
}